/**
 * 用于判断客户端当前请求接口是否需要jwt验证
 */

//get方法不需要验证token的api
const getRegArr = [
  '/api/v1/statistics/offical',
  '/api/v1/setting/classfiys',
  '/api/v1/modules/homepage',
  '/api/v1/modules/latest',
  '/api/v1/module'
];

//post方法不需要验证token的api
const postRegArr = [
  '/api/v1/contact',
  '/api/v1/user/login',
  '/api/v1/user/sms',
  '/api/v1/user/register',
  '/api/v1/user/wxlogin',
  '/api/v1/public/download'
];

//判断请求api是否在正则数组里
const isNonTokenRegApi = (path, arry) => {
  return arry.some((p) => {
    return p == path;
  });
};

//判断当前请求api是否不需要jwt验证
const checkIsNeedToken = (ctx) => {
  var result = false;
  var path = ctx.path;
  if (ctx.method == 'POST') {
    result = isNonTokenRegApi(path, postRegArr);
  }
  if (ctx.method == 'GET') {
    result = isNonTokenRegApi(path, getRegArr);
    if (
      path == '/api/v1/modules' ||
      path == '/api/v1/modules/latest' ||
      path == '/api/v1/module' ||
      path == '/api/v1/modules/homepage'
    ) {
      if (ctx.headers.authorization) {
        result = false;
      } else {
        result = true;
      }
    }
  }
  return result;
};

exports = module.exports = checkIsNeedToken;
